# frozen_string_literal: true

require_relative "qa"

Dir['tasks/*.rake'].each { |file| load file }

desc "Initialize GitLab with an access token"
task :initialize_gitlab_auth, [:address] do |_, args|
  QA::Tools::InitializeGitlabAuth.new(args).run
end

desc "Generate Performance Testdata"
task :generate_perf_testdata, :type do |_, args|
  args.with_defaults(type: :all)
  QA::Tools::GeneratePerfTestdata.new.method(args[:type]).call
end

desc "Run artillery load tests"
task :run_artillery_load_tests do
  unless ENV['HOST_URL'] && ENV['LARGE_ISSUE_URL'] && ENV['LARGE_MR_URL']
    urls_file = ENV['URLS_FILE_PATH'] || 'urls.yml'

    unless File.exist?(urls_file)
      raise(<<~ERR)
        #{urls_file} file is missing. Please provide correct URLS_FILE_PATH or all of HOST_URL, LARGE_ISSUE_URL and LARGE_MR_URL\n
      ERR
    end

    urls = YAML.safe_load(File.read(urls_file))
    ENV['HOST_URL'] = urls["host"]
    ENV['LARGE_ISSUE_URL'] = urls["large_issue"]
    ENV['LARGE_MR_URL'] = urls["large_mr"]
  end

  sh('artillery run load/artillery.yml -o report.json')
  sh('artillery report report.json -o report.html && rm report.json')
end

desc "Generate data and run load tests"
task generate_data_and_run_load_test: [:generate_perf_testdata, :run_artillery_load_tests]

desc "Generate group with multiple projects for direct transfer test"
task :generate_direct_transfer_test_group, [:project_tar_paths, :group_path, :project_copies] do |_, args|
  QA::Support::GitlabAddress.define_gitlab_address_attribute!
  QA::Runtime::Browser.configure!
  QA::Runtime::Scenario.from_env(QA::Runtime::Env.runtime_scenario_attributes)

  numeric_args = { project_copies: Integer(args[:project_copies], exception: false) }.compact
  string_args = args.to_h
    .slice(:project_tar_paths, :group_path)
    .compact_blank

  QA::Tools::GenerateImportTestGroup.new(**string_args, **numeric_args).generate
end
